package com.free.web;

import com.free.common.entity.ReqInfoDto;
import com.free.common.entity.ServiceInstance;
import com.free.common.utils.Result;
import com.free.container.RegisterContainer;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Map;

/**
 * @author hao.ding@insentek.com
 * @date 2021-10-29 14:10
 */
@RequestMapping("/mayikt")
@RestController
@CrossOrigin
@RequiredArgsConstructor
public class RegisterController {

    private final RegisterContainer registerContainer;

    /**
     * 服务接口注册
     *
     * @return
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody ReqInfoDto reqInfo) {
        // 将该服务地址 存放到 Map集合中
        return Result.succeed(registerContainer.put(reqInfo));
    }

    /**
     * 移除接口
     *
     * @param reqInfo
     * @return
     */
    @PostMapping("/remove")
    public Result<?> remove(@RequestBody ReqInfoDto reqInfo) {
        return Result.succeed(registerContainer.remove(reqInfo) != null);
    }

    /**
     * 服务续约接口
     *
     * @param reqInfo
     * @return
     */
    @PostMapping("/renewal")
    public Result<?> renewal(@RequestBody ReqInfoDto reqInfo) {
        return Result.succeed(registerContainer.renewal(reqInfo));
    }

    /**
     * 查询活跃的服务列表
     * http://127.0.0.1:8848/activeService
     *
     * @return
     */
    @GetMapping("/activeService")
    public Map<String, HashSet<ServiceInstance>> activeService() {
        return registerContainer.getMapAddres();
    }
}
